package dome_001;

/**
 * <p>Description: [饥饿死锁]</p>
 * Created on 2020/12/2
 *
 * @author <a href="mailto: 819712047@qq.com">谢振东 </a>
 * @version 1.0
 */
public class Demo001_02 {

    public static void main(String[] args) {

        Thread thread1 = new Thread(new SyncAddRunnable("thread1"));
        Thread thread2 = new Thread(new SyncAddRunnable("thread2"));

        thread1.setName("thread1");
        thread2.setName("thread2");

        thread1.start();
        thread2.start();
    }

    public static class SyncAddRunnable implements Runnable {

        String threadName;

        public SyncAddRunnable(String threadName) {
            this.threadName = threadName;
        }

        @Override
        public void run() {
            System.out.println(String.format("运行线程开始-" + threadName));
            try {
                synchronized (this) {
                    System.out.println(threadName + "已获取锁");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(String.format("运行线程结束-" + threadName));
        }
    }
}
